<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商圈名称"
        >
          <a-input
            placeholder="商圈名称"
            v-decorator="[ 'name', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商圈地址"
        >
          <a-textarea :rows="2" placeholder="商圈地址"
                      v-decorator="['address', {rules: [{ required: true, message: '请输入。。。' }]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司名称"
        >
          <a-input
            placeholder="公司名称"
            v-decorator="['realName', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司电话"
        >
          <a-input
            placeholder="公司电话"
            v-decorator="['phone', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>


        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="小程序Appid"
        >
          <a-input
            placeholder="小程序Appid"
            v-decorator="[ 'miniAppid', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="小程序Secret"
        >
          <a-input
            placeholder="小程序Secret"
            v-decorator="[ 'miniSecret', {rules: [{ required: true, message: '请输入。。。' }]}]"
          ></a-input>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="卷id或url一"
        >
          <a-input id="transUrl1"
                   placeholder="卷id或url一"
                   v-decorator="['transUrl1']"
          ></a-input>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="卷id或url二"
        >
          <a-input id="transUrl2"
                   placeholder="卷id或url二"
                   v-decorator="['transUrl2']"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="卷id或url三"
        >
          <a-input id="transUrl3"
                   placeholder="卷id或url三"
                   v-decorator="['transUrl3']"
          ></a-input>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="轮播图(690*345)"
        >
          <a-upload
            :action="this.$baseURL+'/pc/common/uploadImg'"
            listType="picture-card"
            :fileList="fileList"
            @preview="uploadPreview"
            @change="uploadChange"
            :disabled="flag"
          >
            <div v-if="fileList.length < 3">
              <a-icon type="plus"/>
              <div class="ant-upload-text">上传轮播图</div>
            </div>
          </a-upload>
          <a-modal :visible="previewVisible" :footer="null" @cancel="uploadCancel">
            <img alt="example" style="width: 100%" :src="previewImage"/>
          </a-modal>

        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="背景图(690*340)"
        >
          <a-upload
            :action="this.$baseURL+'/pc/common/uploadImg'"
            listType="picture-card"
            :fileList="backList"
            @preview="uploadPreviewBack"
            @change="uploadChangeBack"
            :beforeUpload="beforeUpload"
            :data="ruleForm"
          >
            <div v-if="backList.length < 1">
              <a-icon type="plus"/>
              <div class="ant-upload-text">上传背景图</div>
            </div>
          </a-upload>
          <a-modal :visible="previewVisible" :footer="null" @cancel="uploadCancel">
            <img alt="example" style="width: 100%" :src="previewImage"/>
          </a-modal>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save, deletePic, selectByMasterId, selectByBackId } from '@/api/system/company'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        flag: false,
        isDisabled: false,

        previewVisible: false,
        previewImage: '',
        fileList: [],
        backList: [],
        transUrl: '',
        ruleForm: {
          height: 690,
          width: 340
        }
      }
    },
    //--############  data() ###############-->

    created() {
    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
        this.fileList = []
        this.backList = []
      },
      edit(record) {
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'name', 'address', 'realName','phone', 'miniAppid', 'miniSecret', 'transUrl1', 'transUrl2', 'transUrl3'))
        })
        selectByMasterId(this.mdl.id).then(res => {
          this.fileList = res.result
        })
        selectByBackId(this.mdl.id).then(res => {
          this.backList = res.result
        })

      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {

        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            /*对背景数据进行重新组装*/
            console.log('==========================')
            console.log(this.fileList)
            let packageFileList1 = packageFileList(this.fileList)
            values.fileList = packageFileList1
            /*对背景数据进行重新组装*/
            let packageFileList2 = packageFileList(this.backList)
            if(packageFileList2.length>0){
              if(packageFileList2[0]==undefined){
                packageFileList2=null
              }
            }
            console.log(packageFileList2);
            values.backList = packageFileList2
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            console.log(values)
            // return
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },

      uploadCancel() {
        this.previewVisible = false
      },
      uploadPreview(file) {
        this.previewImage = file.url || file.thumbUrl
        this.previewVisible = true
      },
      uploadPreviewBack(file) {
        this.previewImage = file.url || file.thumbUrl
        this.previewVisible = true
      },
      uploadChange(info) {
        this.flag = true
        this.fileList = info.fileList
        if (info.file.status === 'done') {
          this.fileList.pop()
          this.fileList.push(info.file)
          this.$message.success(`${info.file.name} 上传成功`)
          this.flag = false
        } else if (info.file.status === 'error') {
          this.$message.error(`${info.file.name} 上传失败`)
        } else if (info.file.status === 'removed') {
          deletePic(info.file.uid).then(res => {

          })
        }
      },
      uploadChangeBack(info) {
        console.log(info)

        this.backList = info.fileList
        if (info.file.status === 'done') {
          this.backList.pop()

          if (info.file.response.uid == null) {
            this.$message.error(`图片宽高必须是690*340`)
            return
          }

          this.backList.push(info.file)
          this.$message.success(`${info.file.name} 上传成功`)
        } else if (info.file.status === 'error') {
          this.$message.error(`${info.file.name} 上传失败`)
        } else if (info.file.status === 'removed') {
          deletePic(info.file.uid).then(res => {

          })
        }
      },
      beforeUpload(file) {
        const isJPEG = file.type === 'image/jpeg'
        const isJPG = file.type === 'image/jpg'
        const isPNG = file.type === 'image/png'
        if (!isJPG && !isJPEG && !isPNG) {
          this.$message.error('只支持文件格式jpg,png')
        }
        return file.type
      }
    }
    //--############  methods() ###############-->
  }

  function packageFileList(fileList) {
    let fileLists = []
    for (let i = 0; i < fileList.length; i++) {
      fileLists.push(fileList[i].response)
    }
    return fileLists
  }
</script>

<style scoped>
  /* you can make up upload button and sample style by using stylesheets */
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
</style>
